package com.logicbus.xscript;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.anysoft.util.Settings;
import com.logicbus.backend.ServantFactory;
import com.logicbus.backend.ServantRegistry;
import com.logicbus.models.catalog.Path;
import com.logicbus.models.servant.ServiceDescription;
import org.apache.commons.lang3.StringUtils;

/**
 * 定位到具体的服务
 *
 * @since 1.6.13.7 [20200812 duanyy] <br>
 */
public class SvcLocate extends Segment {
    protected String $path = "";
    protected boolean doSwagger = false;

    public SvcLocate(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        $path = PropertiesConstants.transform(p,"path",$path);
        doSwagger = PropertiesConstants.getBoolean(p,"swagger",doSwagger);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        String path = PropertiesConstants.transform(ctx,$path,"");
        if (StringUtils.isNotEmpty(path)) {
            Settings settings = Settings.get();
            ServantFactory sf = (ServantFactory) settings.get("servantFactory");
            ServantRegistry sm = sf.getServantRegistry();
            ServiceDescription sd = sm.get(new Path(path));
            if (sd != null){
                LogicletContext childCtx = new LogicletContext(ctx);
                childCtx.SetValue("$id",sd.getServiceID());
                childCtx.SetValue("$name",sd.getName());
                childCtx.SetValue("$note",sd.getNote());
                childCtx.SetValue("$module",sd.getModule());
                childCtx.SetValue("$visible",sd.getVisible());
                childCtx.SetValue("$path",sd.getPath());
                childCtx.SetValue("$log",sd.getLogType().toString());
                childCtx.SetValue("$acGroup",sd.getAcGroup());
                childCtx.SetValue("$privilege", StringUtils.isEmpty(sd.getPrivilege())?sd.getPath():sd.getPrivilege());
                childCtx.SetValue("$desc",
                        String.format("%s\n\n**Module**:<code>%s</code>\n\n**Visible**:<code>%s</code>\n\n**Log Type**:<code>%s</code>\n\n**Access Control Group**:<code>%s</code>\n\n**Privilage**:<code>%s</code>",
                                sd.getNote(),sd.getModule(),sd.getVisible(),sd.getLogType().toString(),sd.getAcGroup(),sd.getPrivilege()));

                String swagger = "false";
                if (doSwagger){
                    Logiclet onSwagger = sd.getSwaggerLogiclet();
                    if (onSwagger != null){
                        onSwagger.execute(root,current,childCtx,watcher);
                        swagger = "true";
                    }
                }
                childCtx.SetValue("$swagger",swagger);
                super.onExecute(root, current, childCtx, watcher);
            }
        }
    }
}